package com.rongxun.hbwallet.ui.adapter;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.constants.IntentKey;
import com.rongxun.hbwallet.constants.SPKey;
import com.rongxun.hbwallet.model.bean.UserCandyInfo;
import com.rongxun.hbwallet.model.entity.SubCate;
import com.rongxun.hbwallet.model.bean.User;
import com.rongxun.hbwallet.ui.activities.DealMessageActivity;
import com.rongxun.hbwallet.ui.activities.LoginActivity;
import com.rongxun.hbwallet.ui.activities.MentionMoneyActivity;
import com.rongxun.hbwallet.ui.activities.SweetDetailActivity;
import com.rongxun.hbwallet.ui.activities.WalletDetailActivity;
import com.rongxun.hbwallet.ui.activities.WalletManagerActivity;
import com.rongxun.utils.ALog;
import com.rongxun.utils.ActivityUtils;
import com.rongxun.utils.ApkVersionUtils;
import com.rongxun.utils.EventConf;
import com.rongxun.utils.EventUtil;
import com.rongxun.utils.MyToast;
import com.rongxun.utils.PreferenceUtils;
import com.rongxun.utils.StatusBarUtil;
import com.rongxun.utils.wallet.ETHWallet;
import com.rongxun.utils.wallet.WalletDaoUtils;
import com.rongxun.widget.ExchangeTokenDialog;

import java.util.List;

/**
 * 创建者     庄丰泽
 * 创建时间   2018/8/16 12:07
 * 描述
 */
public class SelfAdapter extends BaseMultiItemQuickAdapter<SubCate, BaseViewHolder> implements View.OnClickListener, BaseQuickAdapter.OnItemClickListener {
    private final int USER_INFO = 0;
    private final int LIST_TAB = 1;
    private final int LOGIN_OUT = 2;

    private int last;
    private User mUser;
    private String CityMoney_number = "";
    private UserCandyInfo userCandyInfo;
    public void setCityMoney_number(String cityMoney_number) {
        CityMoney_number = cityMoney_number;
    }
    public void setUserCandyInfo(UserCandyInfo userCandyInfo) {
        this.userCandyInfo = userCandyInfo;
    }
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public SelfAdapter(List<SubCate> data) {
        super(data);
        addItemType(USER_INFO, R.layout.item_self_user);
        addItemType(LIST_TAB, R.layout.item_self_listtab);
        addItemType(LOGIN_OUT, R.layout.item_login_out);
        this.setOnItemClickListener(this::onItemClick);
        last = data.size() - 1;
    }

    public void setUser(User mUser) {
        this.mUser = mUser;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.myself_already_token:
                if (!TextUtils.isEmpty(PreferenceUtils.getString(SPKey.TOKEN, ""))) {
                    EventUtil.postInfoEvent(EventConf.EXCHANGE_TOKEN, 0);
                } else {
                    ActivityUtils.startActivityWithAnimotion((Activity) mContext, new Intent(mContext, LoginActivity.class));
                }
                break;
            case R.id.myself_today_sweet:
                ActivityUtils.startActivityWithAnimotion((Activity) mContext, new Intent(mContext, SweetDetailActivity.class));
                break;
            case R.id.myself_today_sweet_hint:
                ActivityUtils.startActivityWithAnimotion((Activity) mContext, new Intent(mContext, SweetDetailActivity.class));
                break;
            case R.id.myself_user_name:
                ActivityUtils.startActivityWithAnimotion((Activity) mContext, new Intent(mContext, LoginActivity.class));
                break;
        }
    }

    @Override
    protected void convert(BaseViewHolder v, SubCate item) {
        switch (v.getItemViewType()) {
            case USER_INFO:
                v.setOnClickListener(R.id.myself_already_token, this::onClick);
                v.setOnClickListener(R.id.myself_today_sweet_hint, this::onClick);
                v.setOnClickListener(R.id.myself_today_sweet, this::onClick);
                TextView user_name = v.getView(R.id.myself_user_name);
                TextView tv = v.getView(R.id.item_self_user_my);
                tv.setPadding(0, StatusBarUtil.getStatusBarHeight(mContext), 0, 0);
                if (mUser != null) {
                    user_name.setText(mUser.getUserInfo().getMobile() +"");
                    ALog.i("my_icon", mUser.getUserInfo().getIcon());
                    user_name.setEnabled(false);
                } else {
                    user_name.setText(mContext.getString(R.string.not_login));
                    user_name.setEnabled(true);
                    user_name.setOnClickListener(this::onClick);
                }
                if(userCandyInfo!=null){
                    if(userCandyInfo.getTodayCandyNum()>0){
                        v.setText(R.id.myself_today_sweet,String.format(mContext.getString(R.string.candy_unit),String.valueOf(userCandyInfo.getTodayCandyNum())));
                    }
                    if(userCandyInfo.getAllHistoryCandyNum()>0){
                        v.setText(R.id.myself_already_sweet,String.format(mContext.getString(R.string.candy_unit),String.valueOf(userCandyInfo.getAllHistoryCandyNum()/1000)));
                    }
                }
                break;
            case LIST_TAB:
                v.setText(R.id.myself_list_title, item.getTitle());
                RelativeLayout rv = v.getView(R.id.self_item_ev);
                ImageView img_right = v.getView(R.id.right_img);
                TextView tv_right = v.getView(R.id.myself_list_hint);
                if (v.getAdapterPosition() == last - 1) {
                    rv.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                    img_right.setVisibility(View.GONE);
                    tv_right.setPadding(0, 0, (int) mContext.getResources().getDimension(R.dimen.x55), 0);
                    tv_right.setText(ApkVersionUtils.getVersionName(mContext));
                } else {
                    rv.setBackground(mContext.getResources().getDrawable(R.drawable.bottom_line));
                    img_right.setVisibility(View.VISIBLE);

                }
                if (v.getAdapterPosition() == 1) {
                    tv_right.setPadding(0, 0, (int) mContext.getResources().getDimension(R.dimen.x85), 0);
                    tv_right.setText(CityMoney_number);
                    tv_right.setTextColor(mContext.getResources().getColor(R.color.light_gold));   //方法三
                }
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        switch (position) {
            case 1:
                StartTypeActivity(MentionMoneyActivity.class);
                break;
            case 2:
                StartTypeActivity(WalletDetailActivity.class);
                break;
            case 3:
                StartTypeActivity(DealMessageActivity.class);
                break;
            case 8:
                if (mUser != null) {
                    mUser = null;
                    SelfAdapter.this.notifyItemChanged(0);
                    PreferenceUtils.reset();
                }
                ActivityUtils.startActivityWithAnimotion((Activity) mContext, new Intent(mContext, LoginActivity.class));
                break;
        }
    }

    private void StartTypeActivity(Class a) {
        if (!TextUtils.isEmpty(PreferenceUtils.getString(SPKey.TOKEN, ""))) {
            ETHWallet ethWallet = WalletDaoUtils.checkRepeatByUserId(PreferenceUtils.getString(SPKey.USERID, "0"));
            if (ethWallet!= null) {
                if(a.getName().equals("com.rongxun.hbwallet.ui.activities.WalletDetailActivity")){
                    ActivityUtils.startActivityWithAnimotion((Activity) mContext, new Intent(mContext, a).putExtra(IntentKey.ETH_WALLET, ethWallet));
                    return;
                }
                if(a.getName().equals("com.rongxun.hbwallet.ui.activities.MentionMoneyActivity")&&!CityMoney_number.equals("")&&!CityMoney_number.equals("0")){
                    ActivityUtils.startActivityWithAnimotion((Activity) mContext, new Intent(mContext, a).putExtra(IntentKey.CITYMONEY_NUMBER, CityMoney_number));
                    return;
                }
                ActivityUtils.startActivityWithAnimotion((Activity) mContext, new Intent(mContext, a));
            } else {
                //没钱包去创建
                ActivityUtils.startActivityWithAnimotion((Activity) mContext, new Intent(mContext, WalletManagerActivity.class));
            }
        } else {
            //去登录
            ActivityUtils.startActivityWithAnimotion((Activity) mContext, new Intent(mContext, LoginActivity.class));
        }
    }
}
